﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Faster.Entity;
using System.IO;
using System.Xml;

namespace Faster.GameBLL
{
    public class PropsBaike
    {

        static PropsBaike instance = null;
        static readonly object padlock = new object();

        public List<PropsInfo> Props
        {
            get;
            set;
        }

        private PropsBaike()
        {

        }       

        public static PropsBaike Instance
        {
            get
            {
                if (instance != null)
                {
                    return instance;
                }
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new PropsBaike();
                    }
                    return instance;
                }
            }
        }

        public void LoadPlants(UserInfo user)
        {
            if (this.Props != null)
            {
                return;
            }
            this.Props = new List<PropsInfo>();

            string url = "http://s1.kaixin001pvz.youkia.com/pvz/index.php/default/organism/sig/cb7f50e72acc462afc7a82ec970ccbc7?1322804299527";

            string xml = RequestHelper.GetRequest(url, user.Cookies);

            TextReader text = new StringReader(xml);
            XmlTextReader read = new XmlTextReader(text);
            read.WhitespaceHandling = WhitespaceHandling.All;
            while (read.Read())
            {
                if (read.Name == "item")
                {
                    PropsInfo plant = new PropsInfo();
                    for (int i = 0; i < read.AttributeCount; i++)
                    {
                        read.MoveToAttribute(i);
                        if (read.Name == "name")
                        {
                            plant.Name = read.Value;
                        }
                        else if (read.Name == "id")
                        {
                            plant.ID = Convert.ToInt32(read.Value);
                        }
                        else if (read.Name == "type")
                        {
                            plant.Type = Convert.ToInt32(read.Value);
                        }                       
                    }
                    read.MoveToElement();
                    this.Props.Add(plant);
                }


            }
            read.Close();
            text.Close();

        }
    }
}
